import { defineStore } from "pinia";
import { reactive } from "vue"
import { type AdminType, type Typeform, type Update_Admin, type Updata_pass } from "@/types/Admin"
import { Admin_info, Token } from "@/utils/Local.ts"
import { admin_login, admin_updata, admin_updata_avatar, admin_updata_password } from "@/apis/UserApi"
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { AppStore } from "@/store/index"

export const AdminStore = defineStore('admin', () => {
    const router = useRouter()

    const Admininfo = reactive<AdminType>({
        _id: "",
        username: "",
        avatar: "",
        bilibili: "",
        csdn: "",
        english_name: "",
        famous_sentence: "",
        gitee: "",
        introduce: "",
        qq: "",
        role: "",
        school: "",
        tags: [],
    })

    const get_info = () => {
        console.log(Admin_info.Get_info())

    }
    const Login = async (formData: Typeform) => {
        try {
            const result: any = await admin_login(formData)
            Object.assign(Admininfo, result.data)
            console.log(result, '的呢路古')
            if (result.code == 200) {
                AppStore().isadmin = true
            }
            //管理员信息保存到本地
            Admin_info.Set_info(Admininfo)
            //保存token
            Token.Set_token(result.token)
            if (result.code == 200) {
                router.push('/back')
                ElMessage({
                    type: "success",
                    message: "登录成功"
                })
            }
        } catch (error) {

        }

    }
    const Update_info = async (Updata: Update_Admin) => {

        try {
            const Admin_locl_info = Admin_info.Get_info()
            Object.assign(Admin_locl_info, Updata)
            Admin_info.Set_info(Admin_locl_info)
            const result = await admin_updata(Updata)
        } catch (error) {

        }
    }
    //更新头像
    const Update_avatar = async (url: string) => {
        try {
            const Admin_locl_info = Admin_info.Get_info()
            Object.assign(Admin_locl_info, {
                avatar: url
            })
            Admin_info.Set_info(Admin_locl_info)
            const result = await admin_updata_avatar(url)
            console.log(result, '结果')
        } catch (error) {
            console.log(error, '错误')

        }
    }
    //更新管理员密码
    const Updata_pass = async (data: Updata_pass) => {
        const result: any = await admin_updata_password(data)
        console.log(result, '初级')
        if (result.code == 200) {
            Token.Re_move()
            Admin_info.Remove_info()
            router.push('/login')
            ElMessage({
                type: "success",
                message: "修改成功"
            })
        } else {
            ElMessage({
                type: "error",
                message: "修改失败"
            })
        }
    }
    return {
        Admininfo,
        get_info,
        Login,
        Update_info,
        Update_avatar,
        Updata_pass
    }
})